import React,{useEffect,useState} from 'react'
import Item from './Item'
import PubSub from 'pubsub-js'


export default function Main() {
//第六步：初始化状态
const [searchList,setSearchList] =useState({isInit:true,isSearch:false,data:[]})//优化初始化state的值，一开始页面展示请开始搜索
useEffect(()=>{
  PubSub.subscribe('searchValue', function(_,data){ 
    setSearchList(data)
  })
},[])
  return (
    <div className="row">
      {/* 第七步 */}
      {
        searchList.isInit?<h1>请开始搜索</h1>:
          searchList.isSearch?<h1>正在搜索中</h1>:
            searchList.data.map((item)=>{
                return <Item key={item.id} {...item}/>
            })
      }
    </div>
  )
}
